package com.googlecode.lookie.platform.image.format;

import java.io.InputStream;

public class FormatResolverImpl
    implements FormatResolver
{

    private FormatRegistry registry;

    public FormatResolverImpl( FormatRegistry registry )
    {
        this.registry = registry;
    }

    /* (non-Javadoc)
     * @see com.googlecode.lookie.platform.image.format.FormatResolver#getFormatByName(java.lang.String)
     */
    public Format getFormatByName( String name )
    {
        String ext = name.substring( name.lastIndexOf( "." ) + 1 ).toLowerCase();
        for ( Format format : registry.getFormats() )
        {
            for ( String formatExt : format.getExtensions() )
            {
                if ( formatExt.equals( ext ) )
                {
                    return format;
                }
            }
        }
        return null;
    }

    /* (non-Javadoc)
     * @see com.googlecode.lookie.platform.image.format.FormatResolver#getFormatByMimeType(java.lang.String)
     */
    public Format getFormatByMimeType( String mimeType )
    {
        for ( Format format : registry.getFormats() )
        {
            if ( format.getMimeType().equals( mimeType ) )
            {
                return format;
            }
        }
        return null;
    }

    /* (non-Javadoc)
     * @see com.googlecode.lookie.platform.image.format.FormatResolver#getFormatByContent(java.io.InputStream)
     */
    public Format getFormatByContent( InputStream is )
    {
        // TODO
        return null;
    }

}
